export default class Geometric {
  constructor(basicCesium) {
     this.basicCesium = basicCesium
    this.viewer = basicCesium.viewer;
    this.entities = this.viewer.entities

    this.initEntity()
  }

  initEntity() {
    let entities = this.viewer.entities.values;
    if (entities.length != 0) return false;
    //盒子
    this.entities.add({
      name: '盒子',
      position: this.getPosition(),
      box: {
        dimensions: new Cesium.Cartesian3(120000.0, 140000.0, 160000.0), // 长、宽、高
        material: Cesium.Color.BLUE,
        fill: true, // 是否填充
        outline: true, // 是否显示边框
        outlineColor: Cesium.Color.BLACK // 边框颜色
      }
    });
    //圆和椭圆
    this.entities.add({
      name: '圆或椭圆',
      position: this.getPosition(),
      ellipse: {
        semiMinorAxis: 80000.0, // 短半轴长度
        semiMajorAxis: 80000.0, // 长半轴长度 
        height: 0.0, // 离地高度
        extrudedHeight: 200000.0, // 拉伸高度  高度-拉伸高度=立体
        rotation: Cesium.Math.toRadians(45), // 旋转
        material: Cesium.Color.YELLOW,
        outline: true // height must be set for outline to display
      }
    });

    //走廊 
    this.entities.add({
      name: '走廊',
      corridor: {
        positions: Cesium.Cartesian3.fromDegreesArray([
          103.0, 31.0,
          108.0, 35.0,
          113.0, 31.0
        ]), // 中心点集
        height: 0.0,
        extrudedHeight: 50000.0, // 拉伸高度 高度 - 拉伸高度 = 立体
        width: 80000.0, // 缓冲带宽  走廊宽度
        cornerType: Cesium.CornerType.MITERED, // BEVELED斜角 MITERED直角 ROUNDED圆角
        material: Cesium.Color.YELLOW.withAlpha(0.5),
        outline: true, // height or extrudedHeight must be set for outlines to display
        outlineColor: Cesium.Color.BLACK
      }
    });

    //气缸和锥体
    this.entities.add({
      name: '气缸和锥体',
      position: this.getPosition(),
      cylinder: {
        length: 200000.0, // 高度
        topRadius: 100000.0, // 顶部的宽度 当topRadius和bottomRadius一致的时，就是气缸
        bottomRadius: 200000.0, // 底部的宽度
        material: Cesium.Color.GREEN.withAlpha(0.5),
        outline: true,
        outlineColor: Cesium.Color.DARK_GREEN
      }
    });

    //多边形 
    this.entities.add({
      name: '多边形',
      polygon: {
        // hierarchy : Cesium.Cartesian3.fromDegreesArray([
        //    106.0, 39.0,
        //    109.0, 38.0, 
        //    110.0, 36.0, 
        //    109.0, 40.0, 
        //    105.0, 40.0,
        // ]), // 统一高度
        // extrudedHeight: 50000.0, // 拉伸高度

        hierarchy: Cesium.Cartesian3.fromDegreesArrayHeights([
          106.0, 39.0, 20000,
          109.0, 38.0, 20000,
          110.0, 40.0, 30000,
          109.0, 40.0, 40000,
          105.0, 40.0, 50000
        ]), // 自定义高度

        extrudedHeight: 0, // 拉伸高度
        perPositionHeight: true,
        closeTop: true, // 多面体顶部是否封闭
        closeBottom: true, // 多面体底部是否封闭
        material: Cesium.Color.GREEN
      }
    });

    //挖洞多边形 
    this.entities.add({
      name: '挖洞多边形',
      polygon: {
        hierarchy: {
          positions: Cesium.Cartesian3.fromDegreesArray([
            106, 42.0,
            112, 42.0,
            112, 48.0,
            106, 48.0
          ]),
          holes: [{
            positions: Cesium.Cartesian3.fromDegreesArray([
              107, 43.0,
              111, 43.0,
              111, 47.0,
              107, 47.0
            ]),
            holes: [{
              positions: Cesium.Cartesian3.fromDegreesArray([
                108, 44.0,
                110, 44.0,
                110, 46.0,
                108, 46.0,
              ]),
            }]
          }]
        },
        material: Cesium.Color.BLUE.withAlpha(0.5),
        height: 0,
        extrudedHeight: 50000, // 拉伸高度
        outline: true // height is required for outline to display
      }
    });

    // 折线 
    this.entities.add({
      name: '折线',
      polyline: {
        positions: Cesium.Cartesian3.fromDegreesArray([
          100, 34.0,
          101, 36.0,
          102, 34.0,
          103, 36.0,
          104, 34.0,
        ]),
        width: 10,
        // material: Cesium.Color.RED // 普通样式
        // material : new Cesium.PolylineGlowMaterialProperty({
        //     glowPower : 0.2, // 发光的宽度 为 线的 倍数
        //     color : Cesium.Color.RED
        // }) // 发光样式

        // material : new Cesium.PolylineOutlineMaterialProperty({
        //     color : Cesium.Color.ORANGE,
        //     outlineWidth : 2,
        //     outlineColor : Cesium.Color.RED
        // }) // 线的边框线

        // material: new Cesium.PolylineArrowMaterialProperty(Cesium.Color.PURPLE), // 带箭头
        // followSurface: true //指定是直线还是弧线==浮在表面

        material: new Cesium.PolylineDashMaterialProperty({
          color: Cesium.Color.CYAN, // 线颜色
          gapColor: Cesium.Color.RED, // 分段颜色==默认透明
          dashLength: 60 // 分段线的长度 16px
        }) // 分段线
      }
    });

    // 折线卷 
    this.entities.add({
      name: '圆',
      polylineVolume: {
        positions: Cesium.Cartesian3.fromDegreesArray([
          104, 37.0,
          100, 38.0,
          98, 40.0
        ]),
        shape: this.computeCircle(60000.0), // 圆
        material: Cesium.Color.RED
      }
    });

    this.entities.add({
      name: '削角',
      polylineVolume: {
        positions: Cesium.Cartesian3.fromDegreesArrayHeights([
          90.0, 32.0, 0.0,
          96.0, 36.0, 100000.0,
          90.0, 36.0, 0.0
        ]),
        shape: [new Cesium.Cartesian2(-50000, -50000),
          new Cesium.Cartesian2(50000, -50000),
          new Cesium.Cartesian2(50000, 50000),
          new Cesium.Cartesian2(-50000, 50000)
        ], // 矩形
        cornerType: Cesium.CornerType.MITERED,
        material: Cesium.Color.GREEN.withAlpha(0.5),
        outline: true,
        outlineColor: Cesium.Color.BLACK
      }
    });

    this.entities.add({
      name: '星星',
      polylineVolume: {
        positions: Cesium.Cartesian3.fromDegreesArrayHeights([
          100.0, 26.0, 0.0,
          102.0, 28.0, 100000.0,
          106.0, 26.0, 200000.0
        ]),
        shape: this.computeStar(7, 70000, 50000), // 星星
        cornerType: Cesium.CornerType.ROUNDED,
        material: Cesium.Color.PURPLE
      }
    });

    // 矩形 
    this.entities.add({
      name: '矩形',
      rectangle: {
        coordinates: Cesium.Rectangle.fromDegrees(94.0, 22.0, 96.0, 27.0),
        material: Cesium.Color.WHITE.withAlpha(0.5),
        rotation: Cesium.Math.toRadians(45), // 旋转
        extrudedHeight: 300000.0, // 拉伸
        height: 0.0, // 高度
        outline: true,
        outlineColor: Cesium.Color.BLACK
      }
    });

    // 球体和椭球体 
    this.entities.add({
      name: '球体',
      position: this.getPosition(),
      ellipsoid: {
        radii: new Cesium.Cartesian3(200000.0, 200000.0, 300000.0), // 长轴、短轴、高度
        material: Cesium.Color.BLACK,
        outline: true,
        outlineColor: Cesium.Color.WHITE,
        fill: true,
        slicePartitions: 12, // 横切
        stackPartitions: 6 // 竖切
      }
    });

    // 墙壁 
    this.entities.add({
      name: '墙壁',
      wall: {
        positions: Cesium.Cartesian3.fromDegreesArray([
          115.0, 49.0,
          112.5, 50.0,
          110.0, 49.0,
          107.5, 50.0,
          105.0, 49.0,
          102.5, 50.0,
          100.0, 49.0,
          97.5, 50.0,
          95.0, 49.0,
          92.5, 50.0,
          90.0, 49.0
        ]),
        maximumHeights: [100000, 200000, 100000, 200000, 100000, 200000, 100000, 200000, 100000, 200000, 100000], // 指定用于墙的顶部的高度数组，而不是每个位置的高度。
        minimumHeights: [0, 100000, 0, 100000, 0, 100000, 0, 100000, 0, 100000, 0], // 指定要用于墙的底部而不是地球表面的高度数组。
        material: Cesium.Color.BLUE.withAlpha(0.5),
        outline: true,
        outlineColor: Cesium.Color.BLACK
      }
    });

    // 点
    this.entities.add({
      name: '点',
      position: Cesium.Cartesian3.fromDegrees(118.804722, 31.976727),
      point: {
        pixelSize: 10,
        color: Cesium.Color.RED,
        outlineColor: Cesium.Color.WHITE,
        outlineWidth: 2
      },
      label: {
        text: '一点点',
        // 文字样式
        font: '16pt sans-serif',
        style: Cesium.LabelStyle.FILL_AND_OUTLINE, //FILL  FILL_AND_OUTLINE OUTLINE
        fillColor: Cesium.Color.RED,
        // 边框
        outlineColor: Cesium.Color.WHITE,
        outlineWidth: 4,
        // 背景
        showBackground: true, //指定标签后面背景的可见性
        backgroundColor: new Cesium.Color(0.165, 0.165, 0.165, 0.8), // 背景颜色
        backgroundPadding: new Cesium.Cartesian2(5, 5), //指定以像素为单位的水平和垂直背景填充padding
        // 尺寸
        scale: 1.0, //指定应用于文本的比例
        // 位置
        horizontalOrigin: Cesium.HorizontalOrigin.LEFT, // CENTER LEFT RIGHT
        verticalOrigin: Cesium.VerticalOrigin.BASELINE, //BASELINE BOTTOM CENTER TOP
        // 偏移
        eyeOffset: new Cesium.Cartesian3(100, 100, 100), //指定眼睛偏移的属性
        pixelOffset: new Cesium.Cartesian2(10, 0) //指定像素偏移量的属性
      }
    });

    // 广告牌 可以显示一个图片
    this.entities.add({
      name: "广告牌",
      position: Cesium.Cartesian3.fromDegrees(140, 36),
      //广告牌
      billboard: {
        image: 'img/avatar.jpg',
        width: 80,
        height: 100,
        // scale: 1.0, //指定应用于图像大小的比例
        // horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
        // verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        // pixelOffset: new Cesium.Cartesian2(0, 10), // 偏移
        // rotation: 0, // 旋转
        imageSubRegion: new Cesium.BoundingRectangle(25, 25, 50, 50), //(x，y，宽度，高度) 用于广告牌的图像的子区域，而不是整个图像，以像素为单位从左下角开始测量。
        // sizeInMeters: true //指定此广告牌的尺寸是否应以米为单位进行测量
      },
      label: {
        text: '三块广告牌',
        font: '12pt monospace',
        style: Cesium.LabelStyle.FILL_AND_OUTLINE,
        outlineWidth: 2,
        verticalOrigin: Cesium.VerticalOrigin.TOP,
        pixelOffset: new Cesium.Cartesian2(0, 30)
      }
    });
  }
  // 计算星状的位置
  computeStar(arms, rOuter, rInner) {
    var angle = Math.PI / arms;
    var length = 2 * arms;
    var positions = new Array(length);
    for (var i = 0; i < length; i++) {
      var r = (i % 2) === 0 ? rOuter : rInner;
      positions[i] = new Cesium.Cartesian2(Math.cos(i * angle) * r, Math.sin(i * angle) * r);
    }
    return positions;
  }
  // 根据圆长计算每个点的位置
  computeCircle(radius) {
    var positions = [];
    for (var i = 0; i < 360; i++) {
      var radians = Cesium.Math.toRadians(i);
      positions.push(new Cesium.Cartesian2(radius * Math.cos(radians), radius * Math.sin(radians)));
    }
    return positions;
  }

  clearAll() {
    this.entities.clearAll();
  }

  getPosition(alt = 0) {
    let longitude = 118.804722;
    let latitude = 31.976727;
    let lng = longitude + Cesium.Math.nextRandomNumber() * 20;
    let lat = latitude - Cesium.Math.nextRandomNumber() * 10;
    return new Cesium.Cartesian3.fromDegrees(lng, lat, alt);
  }
}